<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class PartyUser extends Model
{
    use SoftDeletes;

    protected $table = 'party_users';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id', 'created_at', 'deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates = [
        'created_at', 'updated_at', 'deleted_at'
    ];

    protected $appends = ['avatar_url'];

    public function getAvatarUrlAttribute()
    {
        return config('app.img_url') . $this -> avatar;
    }


    /**
     * 获取
     * @param int $region_id
     * @param string $select
     * @return array
     */
    public static function getPartyUserByRegion(int $region_id = 0, string $select = '*', int $take = 10): array
    {

        $region_info = self::where('status', 1)
            ->where('region_id', $region_id)
            ->selectRaw($select)
            ->take($take)
            ->get();

        if (isset($region_info) && !empty($region_info)) {

            return $region_info->toArray();

        } else {

            return [];

        }

    }


    /**
     * @param int $region_id
     * @param int $pageNum
     * @param int $pageSize
     * @param string $select
     * @return array
     */
    public static function getPartyUserPaginateByRegion(int $region_id = 0, int $pageNum = 10, int $pageSize = 10, string $select = '*'): array
    {

        $count = self::where('region_id', $region_id)
            ->where('status', 1)
            ->count();

        if ($count < 1) {
            return [];
        }

        if (empty($pageSize) || $pageSize > 100 || $pageSize < 1) {
            $pageSize = 10;
            $limit_size = 10;
        } else {
            $limit_size = $pageSize;
        }
        $total_page = ceil($count / $pageSize);
        if ($pageNum > $total_page) {
            return [];
        }

        if (empty($pageNum) || $pageNum < 1) {
            $pageNum = 1;
            $start = 0;
        } else {
            $start = $pageNum - 1;
        }
        $limit_start = $start * $limit_size;

        $party_user = self::where('region_id', $region_id)
            ->where('status', 1)
            ->selectRaw($select)
            ->offset($limit_start)
            ->limit($limit_size)
            ->get();

        if ($party_user->isEmpty()) {
            return [];
        }

        return [
            #   数据
            'rows' => $party_user->toArray(),
            #   分页信息
            'pages' => [
                'now_page' => $pageNum,
                'total_page' => $total_page,
                'record_count' => $count
            ]
        ];

    }


    /*党总支列表*/
    public static function partyUsers($request){

        $res = self::select('id','username','avatar','identity','address')
            -> where('region_id',$request -> input('region_id')) -> where('status',1) -> get();
        return $res;
    }
}
